package department

import (
	"SmartSearch/internal/models"
	"SmartSearch/internal/repositories"
	"SmartSearch/internal/responses"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
)

type Controller struct {
	departmentRepository *repositories.DepartmentRepository
}

func NewDepartmentController() *Controller {
	return &Controller{
		departmentRepository: &repositories.DepartmentRepository{},
	}
}

func (d *Controller) Add(c *gin.Context) {
	var params struct {
		Name     string `json:"name"`
		ParentId *int64 `json:"parentId"`
	}
	if err := c.BindJSON(&params); err != nil {
		c.JSON(http.StatusOK, responses.GenFailResult("参数解析错误"))
		return
	}
	if len(params.Name) == 0 {
		c.JSON(http.StatusOK, responses.GenFailResult("部门名不能为空"))
		return
	}
	if department, err := d.departmentRepository.FindByName(params.Name); err == nil {
		if department != nil {
			c.JSON(http.StatusOK, responses.GenFailResult("部门已经存在"))
			return
		}
	}
	var parentDepartment *models.Department
	if *params.ParentId > 0 {
		var err error
		if parentDepartment, err = d.departmentRepository.FindById(*params.ParentId); err != nil {
			c.JSON(http.StatusOK, responses.GenFailResult("上级部门不存在"))
			return
		}
	}
	department := &models.Department{
		Name:        params.Name,
		ParentId:    params.ParentId,
		CreatedDate: time.Now(),
	}
	if err := d.departmentRepository.Add(parentDepartment, department); err != nil {
		c.JSON(http.StatusOK, responses.GenFailResult("添加部门失败"))
		return
	}
	c.JSON(http.StatusOK, responses.GenSuccessResult(nil))
}

func (d *Controller) Del(c *gin.Context) {
	var body struct {
		Id int64 `json:"id"`
	}
	if err := c.BindJSON(&body); err != nil {
		c.JSON(http.StatusOK, responses.GenFailResult("参数解析错误"))
		return
	}
	if _, err := d.departmentRepository.FindById(body.Id); err != nil {
		c.JSON(http.StatusOK, responses.GenFailResult("部门不存在"))
		return
	}
	departments, _ := d.departmentRepository.FindSonById(body.Id)
	if len(departments) > 0 {
		c.JSON(http.StatusOK, responses.GenFailResult("存在子部门，无法删除"))
		return
	}
	if err := d.departmentRepository.Del(body.Id); err != nil {
		c.JSON(http.StatusOK, responses.GenFailResult("部门删除失败"))
		return
	}
	c.JSON(http.StatusOK, responses.GenSuccessResult(nil))
}

func (d *Controller) List(c *gin.Context) {
	name := c.Query("name")
	parentId, _ := strconv.ParseInt(c.DefaultQuery("parentId", "-1"), 10, 64)
	current, _ := strconv.ParseInt(c.DefaultQuery("current", "0"), 10, 32)
	pageSize, _ := strconv.ParseInt(c.DefaultQuery("pageSize", "10"), 10, 32)
	list := d.departmentRepository.FindList(name, parentId, int32(current), int32(pageSize))
	c.JSON(http.StatusOK, responses.GenSuccessResult(list))
}

func (d *Controller) Update(c *gin.Context) {
	var params struct {
		Id   int64  `json:"id"`
		Name string `json:"name"`
	}
	if err := c.BindJSON(&params); err != nil {
		c.JSON(http.StatusOK, responses.GenFailResult("参数解析错误"))
		return
	}
	department, err := d.departmentRepository.FindById(params.Id)
	if err != nil {
		c.JSON(http.StatusOK, responses.GenFailResult("部门不存在"))
		return
	}
	if department.Name != params.Name && len(params.Name) != 0 {
		if _, err := d.departmentRepository.FindByName(params.Name); err == nil {
			c.JSON(http.StatusOK, responses.GenFailResult("部门已经存在"))
			return
		} else {
			department.Name = params.Name
		}
	}
	if err := d.departmentRepository.Update(department); err != nil {
		c.JSON(http.StatusOK, responses.GenFailResult("部门修改失败"))
		return
	}
	c.JSON(http.StatusOK, responses.GenSuccessResult(nil))
}
